import { Injectable } from '@nestjs/common';
import { CreateWebConfigDto } from './dto/web-config.dto';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { WebConfigEntity } from './entities/web-config.entity';

@Injectable()
export class WebConfigService {
  constructor(
    @InjectRepository(WebConfigEntity)
    private webConfigEntityRepository: Repository<WebConfigEntity>,
  ) { }
  async getWebConfig() {
    const data = await this.webConfigEntityRepository.find({
      take: 1,
      order: { id: 'ASC' },
    });
    if (data[0]) {
      Object.assign(data[0], { date_format: 'YYYY-MM-DD ' })
    }
    return data[0] ?? {};
  }
  async setWebConfig(data: CreateWebConfigDto): Promise<void> {
    const webConfig = new WebConfigEntity();
    webConfig.id = data.id;
    webConfig.title = data.title;
    webConfig.subtitle = data.subtitle;
    webConfig.author = data.author;
    webConfig.description = data.description;
    webConfig.keywords = data.keywords;
    webConfig.language = data.language;
    await this.webConfigEntityRepository.save(webConfig);
  }

}
